package com.brillilab.service.core.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.crm.LabUserDataMapper;
import com.brillilab.domain.po.crm.LabUserData;
import com.brillilab.service.core.crm.ILabUserDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * <p>
 * 实验室用户数据 服务实现类
 * </p>
 *
 * @author linzhikang
 * @since 2019-09-29
 */
@Service
@Transactional
public class LabUserDataServiceImpl implements ILabUserDataService {

    @Resource
    private LabUserDataMapper labUserDataMapper;

    @Override
    public LabUserData selectByTypeAndDate(Integer type, LocalDate recordDate) {
        LambdaQueryWrapper<LabUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabUserData::getType, type);
        wrapper.apply("date(record_date) = {0}", recordDate);
        //todo 处理多数据问题
        wrapper.orderByAsc(LabUserData::getId);
        wrapper.last("limit 0,1");
        return labUserDataMapper.selectOne(wrapper);
    }

    @Override
    public LabUserData insert(LabUserData labUserData) {
        int insert = labUserDataMapper.insert(labUserData);
        Assert.isTrue(insert == 1, "操作失败！");
        return labUserData;
    }

    @Override
    public boolean update(LabUserData labUserData) {
        return labUserDataMapper.updateById(labUserData) > 0;
    }
}
